#include "graphviewer.h"

GraphViewer::GraphViewer(QWidget *parent)
    : QWidget(parent)
{
    InitWindow();
    InitSignalSlots();
}

GraphViewer::~GraphViewer()
{

}

void GraphViewer::InitWindow()
{
    this->resize(400, 500);

    QGridLayout *grid = new QGridLayout(this);
    QGridLayout *topGrid = new QGridLayout(this);
    QGridLayout *bottomGrid = new QGridLayout(this);

    QGroupBox *topBox = new QGroupBox(this);
    QGroupBox *bottomBox = new QGroupBox(this);

    buttonReadText  = new QPushButton("Read", this);
    buttonView1     = new QPushButton("Visual view", this);
    buttonView2     = new QPushButton("Data view", this);
    buttonReadFile  = new QPushButton("Load", this);
    buttonWriteFile = new QPushButton("Save", this);

    textInputMatrix = new QTextEdit(this);
    textOutputFile  = new QLineEdit(this);
    textInputFile   = new QLineEdit(this);

    QLabel *labelMatrix = new QLabel("Matrix", this);
    QLabel *labelLoad = new QLabel("Loading path", this);
    QLabel *labelSave = new QLabel("Saving path ", this);

    labelMatrix->setFixedHeight(11);

    topGrid->addWidget(labelMatrix, 0, 0);
    topGrid->addWidget(textInputMatrix, 1, 0, 4, 1);
    bottomGrid->addWidget(labelLoad, 0, 0);
    bottomGrid->addWidget(textInputFile, 1, 0);
    bottomGrid->addWidget(labelSave, 2, 0);
    bottomGrid->addWidget(textOutputFile, 3, 0);



    topGrid->addWidget(buttonReadText, 1, 1);
    topGrid->addWidget(buttonView1, 2, 1);
    topGrid->addWidget(buttonView2, 3, 1);
    bottomGrid->addWidget(buttonReadFile, 1, 1);
    bottomGrid->addWidget(buttonWriteFile, 3, 1);

    topBox->setLayout(topGrid);
    bottomBox->setLayout(bottomGrid);
    grid->addWidget(topBox, 0, 0);
    grid->addWidget(bottomBox, 1, 0);
    grid->setVerticalSpacing(0);
    grid->setRowMinimumHeight(1, 20);
    grid->setRowStretch(0, 100);
    grid->setRowStretch(1, 0);
}

void GraphViewer::InitSignalSlots()
{
    QObject::connect(buttonReadText,  SIGNAL(clicked()), this, SLOT(GetGraphModelFromTextEdit()));
    QObject::connect(buttonReadFile,  SIGNAL(clicked()), this, SLOT(FillTextEditFromFile()));
    QObject::connect(buttonWriteFile, SIGNAL(clicked()), this, SLOT(SaveGraphsToFile()));
    QObject::connect(buttonView2,     SIGNAL(clicked()), this, SLOT(OpenTextViewWindow()));
    QObject::connect(buttonView1,     SIGNAL(clicked()), this, SLOT(OpenVisualViewWindow()));
}

void GraphViewer::GetGraphModelFromTextEdit()
{
    if (textInputMatrix->toPlainText().isEmpty())
        QMessageBox::warning(0, "Warning", "Nothing to read.");
    else
        if (Graphs.addGraphsToList(textInputMatrix->toPlainText()))
            textInputMatrix->clear();
}

void GraphViewer::FillTextEditFromFile()
{
    QString Path = textInputFile->text();
    if (!Path.size()) {
        Path = QFileDialog::getOpenFileName(this, "Choose File", "/", "*.txt");
        textInputFile->insert(Path);
    }
    QFile File(Path);
    if (!File.exists() && Path.size()) {
        QMessageBox::warning(0, "Warning", "Incorrect Path.");
        return;
    }
    File.open(QIODevice::ReadWrite | QIODevice::Text);
    QTextStream Stream(&File);
    QString fileContent = Stream.readAll();
    textInputMatrix->clear();
    textInputMatrix->insertPlainText(fileContent);
    File.close();
}

void GraphViewer::SaveGraphsToFile()
{
    QString Path = textOutputFile->text();
    if (!Path.size()) {
        Path = QFileDialog::getOpenFileName(this, "Choose File", "/", "*.txt");
        textOutputFile->insert(Path);
    }
    QFile File(Path);
    if (!File.exists() && Path.size()) {
        QMessageBox::warning(0, "Warning", "Incorrect Path.");
        return;
    }
    File.open(QIODevice::ReadWrite | QIODevice::Text);
    QTextStream Stream(&File);
    for (int i = 0; i < Graphs.size(); ++i)
    {
        GraphModel curModel = Graphs.getGraphModel();
        int vertex = curModel.getVertexCount();
        Stream << vertex;
        Stream << "\n";
        for (int k = 0; k < vertex; ++k) {
            for (int j = 0; j < vertex; ++j) {
                Stream << curModel.getMatrixEl(k, j);
                Stream << " ";
            }
            Stream << "\n";
        }

        Graphs.increaseGraphNum();
    }
    File.close();
}

void GraphViewer::OpenTextViewWindow()
{
    if (Graphs.isEmpty()) QMessageBox::warning(0, "Warning", "Graphs list is empty.");
    else {
        textViewWindow = new GraphTextView(this, &Graphs);
        this->hide();
    }
}

void GraphViewer::OpenVisualViewWindow() {
    if (Graphs.isEmpty()) QMessageBox::warning(0, "Warning", "Graphs list is empty.");
    else {
        visualViewWindow = new GraphVisualView(this, &Graphs);
        this->hide();
    }
}
